package controllers.${model.packageName}.${model.modulePackage};

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import net.sf.json.JSONObject;
import models.${model.tableName}; 
import constants.Constants; 
import controllers.UserController;
import business.${model.model};
import utils.ErrorInfo;
import business.User;
import utils.NumberUtil;
import utils.PageBean;
import utils.Security;
#foreach( $field in $fieldList )
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
import models.${field.inputValueOne};
#end
#end

/**
 * ${model.comment} 控制器
 * 
 * @author ${model.author}
 * @date ${date}
 */ 
public class ${model.model}Action extends UserController{
	
	 
	/**
	 * 查询${model.comment}列表
	 */
	public static void list(){
		ErrorInfo error = new ErrorInfo();
#foreach( $field in $fieldList )
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
		List<${field.inputValueOne}> ${field.field}_list = ${field.inputValueOne}.find("delete_status =0").fetch();
#end
#end	
		render(error#foreach( $field in $fieldList )#if($field.inputType == "select"&&${field.inputValueTwo} != ""),${field.field}_list#end#end);

	}
	/**
	 * 查询${model.comment}列表
	 */
	public static void listJson(){
	
		ErrorInfo error = new ErrorInfo();
		Map map = new HashMap();
		map.put("currPage",params.get("currPage"));
		map.put("pageSize",params.get("pageSize"));
		map.put("orderTypeStr",params.get("orderTypeStr"));//排序create_time desc
#foreach( $field in $fieldList )
#if(${field.matchType}=="any"||${field.matchType}=="eq"||${field.matchType}=="left"||${field.matchType}=="right")
		map.put("${field.field}",params.get("${field.field}"));
#elseif(${field.matchType}=="dateRange")        
		map.put("beginTime_${field.field}",params.get("beginTime_${field.field}"));
		map.put("endTime_${field.field}",params.get("endTime_${field.field}"));
#elseif(${field.matchType} == "select")
		map.put("${field.field}",params.get("${field.field}"));
#end
#end
		 
		PageBean<Map<String,Object>> pageBean = ${model.model}.query${model.model}Page(map, error);
 		
		renderJSON(pageBean);
	}
	
	
	/**
	 * 根据${model.comment}ID查询详情
	 * @param id
	 */
	public static void show(String sign){
		ErrorInfo error = new ErrorInfo();
		long id = Integer.valueOf(sign);
		
		if (error.code < 0) {
			flash.error("请选择一条的记录"); 
		}
		//${model.model} ${model.modelL} = new ${model.model}();
		//${model.modelL}.id = id;
		//render(${model.modelL});
		
		Map map = new HashMap();
		map.put("id",sign);
		PageBean<Map<String,Object>> pageBean = ${model.model}.query${model.model}Page(map, error);
		Map<String,Object> ${model.modelL} = pageBean.page.get(0);
		render(${model.modelL});
	}
	
	/**
	 * 添加${model.comment}页面
	 */
	public static void addPage() {
		ErrorInfo error = new ErrorInfo();
#foreach( $field in $fieldList )
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
		List<${field.inputValueOne}> ${field.field}_list = ${field.inputValueOne}.find("delete_status =0").fetch();
#end
#end	
		render(error#foreach( $field in $fieldList )#if($field.inputType == "select"&&${field.inputValueTwo} != ""),${field.field}_list#end#end);
	}
	
	/**
	 * 添加${model.comment}
	 */
	public static void add(${model.tableName} ${model.modelL}) {
		
		ErrorInfo error = new ErrorInfo();
		${model.model} entity = new ${model.model}();
		entity.createUser =  User.currUser().getId();
		entity.add${model.model}(1,${model.modelL},error);
		 
		if(error.code < 0) {
			error.msg = "添加失败";
		}
		
		renderJSON(error);
	}
	
	
	/**
	 * 编辑${model.comment}页面
	 */
	public static void editPage(String sign) {
		ErrorInfo error = new ErrorInfo();
		 long id = Integer.valueOf(sign);
		
		if (error.code < 0) {
		 
			flash.error("请选择一条的记录"); 
		 
		}
		${model.model} ${model.modelL} = new ${model.model}();
		${model.modelL}.id = id;
#foreach( $field in $fieldList )
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
		List<${field.inputValueOne}> ${field.field}_list = ${field.inputValueOne}.find("delete_status =0").fetch();
#end
#end		
		render(${model.modelL}#foreach( $field in $fieldList )#if($field.inputType == "select"&&${field.inputValueTwo} != ""),${field.field}_list#end#end);
	}
	
	
	/**
	 * 编辑
	 */
	public static void edit(${model.tableName} ${model.modelL},String sign) {
		 
		 ErrorInfo error = new ErrorInfo();
		 
		long id = Security.checkSign(sign, Constants.SUPERVISOR_ID_SIGN, Constants.VALID_TIME, error);
		
		if (error.code < 0) {
			renderJSON(error);
		}
		 
//		if(StringUtils.isBlank(${model.modelL}.name)) {
//			error.msg = "不能为空";
//			renderJSON(error);
//		}
		
		${model.model} entity = new ${model.model}();
#foreach ($field in $fieldList)
#if(${field.field}!="id"&&${field.field}!="createUser"&&${field.field}!="createTime"&&${field.field}!="updateUser"&&${field.field}!="updateTime"&&${field.field}!="deleteStatus")
		entity.${field.field} = ${model.modelL}.${field.columnName};// ${field.comment}
#end
#end
		entity.updateUser =  User.currUser().getId();
		entity.updateTime = new Date();
		entity.edit(1,id,error);
		
		if(error.code < 0) {
			error.msg = "编辑失败";
		}
		 
		renderJSON(error);
	}
	/**
	 * 删除
	 */
	public static void delete(String sign) {
		ErrorInfo error = new ErrorInfo();
		long id = Integer.valueOf(sign);
		
		if (error.code < 0) {
			renderJSON(error);
		} 
		
		${model.model}.delete(1,id,error);
		 
		renderJSON(error);
	}
	/**
	 * 批量删除
	 */
	public static void deleteBatch(String ids) {
		if (StringUtils.isBlank(ids)) {
			flash.error("请选择要删除的记录"); 
		}
		
		String[] arrIds = ids.split(",");
		 
		ErrorInfo error = new ErrorInfo();
		${model.model}.delete(1,arrIds, error);

		renderJSON(error);
	}
	
}
